//
//  ViewController.m
//  简单计算器
//
//  Created by student on 16/2/19.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) UIView *orangeView;

@property (nonatomic,weak) UITextView *textView1;

@property (nonatomic,weak) UITextView *textView2;

@property (nonatomic,weak) UITextView *textView3;

@property (nonatomic,weak) UIButton *calculateBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setUpUI];
}
//搭建界面
-(void)setUpUI{
    self.orangeView = [self setUpView];
    self.textView1 = [self setUpTextViewWithframe:CGRectMake(20, 85, 50, 30) WithbackgroundColor:[UIColor whiteColor]];
    self.textView2 = [self setUpTextViewWithframe:CGRectMake(120, 85, 50, 30) WithbackgroundColor:[UIColor whiteColor]];
    self.textView3 = [self setUpTextViewWithframe:CGRectMake(220, 85, 50, 30) WithbackgroundColor:nil];
    [self setUpLabelWithText:@"+" WithFrame:CGRectMake(85, 85, 20, 30)];
    [self setUpLabelWithText:@"=" WithFrame:CGRectMake(185, 85, 20, 30)];
    self.calculateBtn = [self setUpBtnWithFrame:CGRectMake(190, 150, 50, 50) WithText:@"计算"];
    [self.calculateBtn addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];
    
}
//搭建界面中的orangeView
-(UIView *)setUpView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 414, 200)];
    [view setBackgroundColor:[UIColor orangeColor]];
    [self.view addSubview:view];
    return view;
}
//搭建界面中的TextView
-(UITextView *)setUpTextViewWithframe:(CGRect)frame
          WithbackgroundColor:(UIColor *)color{
    UITextView *textView = [[UITextView alloc] initWithFrame:frame];
    textView.backgroundColor = color;
    [textView setTextAlignment:NSTextAlignmentCenter];
    [self.orangeView addSubview:textView];
    return textView;
}
//搭建界面中的Label
-(void)setUpLabelWithText:(NSString *)text
                  WithFrame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:text];
    [self.orangeView addSubview:label];
}
//搭建界面中Button
-(UIButton *)setUpBtnWithFrame:(CGRect)frame
                WithText:(NSString *)text{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.orangeView addSubview:btn];
    return btn;
}
//按钮的点击事件
-(void)calculate{
    int s1 = [self.textView1.text intValue];
    int s2 = [self.textView2.text intValue];
    int s3 = s1 + s2;
    [self.textView3 setText:[NSString stringWithFormat:@"%i",s3]];
}
//点击屏幕放下键盘
-(IBAction)hideKeyboard{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
